import random
class fenshu:
    def __init__(self, shu=0, fenmu=1):
        if isinstance(shu, int):
            if shu==0:
                self.fenzi = random.randint(1,100)
            else:
                self.fenzi = shu
                self.fenmu = fenmu
        elif isinstance(shu, str):
            if '\'' in shu:
                li = shu.split('\'', 2)
                zheng = int(li[0])
                self.zhenString2fenshu(li[1])
                self.fenzi += zheng*self.fenmu
            elif '/' in shu:
                self.zhenString2fenshu(shu)
            else:
                self.fenmu = 1
                self.fenzi = int(shu)

    def zhenString2fenshu(self, string:str):
        if string[0]=='(' and string[-1]==')':
            string = string[1:-1]
        li = string.split('/')
        assert len(li)==2
        self.fenzi = int(li[0])
        self.fenmu = int(li[1])

    def gongyue(self):
        a = max(self.fenzi, self.fenmu)
        b = min(self.fenzi, self.fenmu)
        while b:
            a,b=b,a%b
        return a
    def yuefen(self):
        maxGongYue = int(self.gongyue())
        self.fenzi //= maxGongYue
        self.fenmu //= maxGongYue

    def __add__(self, other):
        assert isinstance(other, fenshu)
        re = fenshu()
        re.fenzi = self.fenzi*other.fenmu + other.fenzi*self.fenmu
        re.fenmu = self.fenmu*other.fenmu
        re.yuefen()
        return re
    def __sub__(self, other):
        assert isinstance(other, fenshu)
        re = fenshu()
        re.fenzi = self.fenzi*other.fenmu - other.fenzi*self.fenmu
        assert re.fenzi>0
        re.fenmu = self.fenmu*other.fenmu
        re.yuefen()
        return re
    def __mul__(self, other):
        assert isinstance(other, fenshu)
        re = fenshu()
        re.fenzi = self.fenzi*other.fenzi
        re.fenmu = self.fenmu*other.fenmu
        re.yuefen()
        return re
    def __truediv__(self, other):
        assert isinstance(other, fenshu)
        assert other.fenzi>0
        re = fenshu()
        re.fenzi = self.fenzi * other.fenmu
        re.fenmu = self.fenmu * other.fenzi
        re.yuefen()
        return re
    def toFakeString(self):
        self.yuefen()
        if self.fenmu!=1:
            re = str(self.fenzi)+"/"+str(self.fenmu)
        else:
            re = str(self.fenzi)
        return re

    def toDieString(self):
        self.yuefen()
        if self.fenmu!=1:
            if self.fenzi<self.fenmu:
                return self.toFakeString()
            else:
                return str(self.fenzi//self.fenmu)+"'"+str(self.fenzi%self.fenmu)+"/"+str(self.fenmu)
        else:
            return self.toFakeString()

def calc(a: fenshu, b: fenshu, c: str):
    assert c in ['+','-','*','/']
    if c=='+':
        return a+b
    elif c=='-':
        return a-b
    elif c=='*':
        return a*b
    elif c=='/':
        return a/b
    return -1
        
    
